<?php
namespace app\admin\controller;

use app\common\model\LuntanArticle  as ArticleModel;
use app\common\model\LuntanCategory as CategoryModel;
use app\common\model\User           as UserModel;
use app\common\controller\AdminBase;
use think\Session;

/**
 * 文章管理
 * Class Article
 * @package app\admin\controller
 */
class LuntanArticle extends AdminBase
{
    protected $article_model;
    protected $category_model;
    protected $user_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->article_model  = new ArticleModel();
        $this->category_model = new CategoryModel();
        $this->user_model     = new UserModel();

        $category_level_list = $this->category_model->getLevelList();
        $this->assign('category_level_list', $category_level_list);
    }

    /**
     * 文章管理
     * @param int    $cid     分类ID
     * @param string $keyword 关键词
     * @param int    $page
     * @return mixed
     */
    public function index($cid = 0, $keyword = '', $page = 1)
    {
        $category_list = $this->category_model->column('name', 'id');
        $articlelist   = $this->article_model->getArticleList($cid,$keyword,$page);
        return $this->fetch('index', ['cid' => $cid, 'keyword' => $keyword,'articlelist'=>$articlelist,'category_list'=>$category_list]);
    }

    /**
     * 添加文章
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存文章
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'LuntanArticle');
            if(array_key_exists('photo', $data)){
                $data['photo'] = setphoto($data['photo']);
            }
            if($data['is_user']){
                $data['uid'] = Session::get('admin_id');
            }
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->article_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑文章
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $article = $this->article_model->find($id);
        if($article['photo']){
            $article['photo'] = getphoto($article['photo']);
        }
        return $this->fetch('edit', ['article' => $article]);
    }

    /**
     * 更新文章
     * @param $id
     */
    public function update($id)
     {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'LuntanArticle');
            if(array_key_exists('photo', $data)){
                $data['photo'] = setphoto($data['photo']);
            }
            if ($validate_result !== true) {
                  $this->error($validate_result);
            } else {
                if ($this->article_model->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                   $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除文章
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = [])
    {
        $this->delete2($id,$ids,$model = "article_model");
    }

    /**
     * 文章审核状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function statusedit($ids = [], $type = '')
    {
        $this->toggle1($ids,$type,$model = "article_model");
    }

    /**
     * 文章置顶状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function topedit($ids = [], $type = '')
    {
        $this->zhiding1($ids,$type,$model = "article_model");
    }

    /**
     * 文章加精状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function hotedit($ids = [], $type = '')
    {
        $this->hot1($ids,$type,$model = "article_model");
    }

    /**
     * 文章详情页面加载
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function detail($id,$page = 1)
    {
        //获得用户文章信息
        $articleInfo = $this->article_model->getArticle($id);
        //查询文章的回复以及跟帖
        $replyinfo   = $this->article_model->getReplyInfo($id,$page);
        $userlist    = $this->user_model->column('username','id');
        return $this->fetch('check1',['articlelist'=>$articleInfo,'replyinfo'=>$replyinfo,'page'=>$page,'userlist'=>$userlist]);
    }
}